<?php
/**
 * Nadëb (Makú-Nadëb)
 *
 * @filesource 
 * @copyright  Copyright 2010 mateusweb.com.br
 * @license    Freeware
 * @package    Nadeb_Controller
 * @subpackage Nadeb.Controller
 * @version    1.0
 */

class Nadeb_Controller_Configs
{
	public static function save($params,$type = "basic")
	{
		$crud = call_user_func( array( "Nadeb_Controller_Configs", $type ), $params );
		
		Nadeb_Savefile::save($_SERVER['DOCUMENT_ROOT'] . '/application/ini/admin/forms/' . $params['contro_name'] . '.ini', $crud);
	}
	
	public static function basic($params)
	{
		$var    = new $params['modelo'];
		$table  = $var->info();
		
		$crud[] = '[crudconfig]' . "\n";
		$crud[] = 'table.name = "' . $params['tb_name'] . '"' . "\n";
		$crud[] = 'table.key = "' . $params['id_name'] . '"' . "\n";
		$crud[] = "\n";
		$crud[] = 'form.method = "post"' . "\n";
		$crud[] = 'form.id = "f'. $params['crud_name'] .'"' . "\n";
		$crud[] = 'form.class = "f'. $params['crud_name'] .'"' . "\n";
		$crud[] = 'form.enctype = "multipart/form-data"' . "\n";
		$crud[] = 'form.action = "/admin/' . $params['contro_name'] . '/insert"' . "\n";
		$crud[] ="\n";
		for($i = 1;$i < (count($table["cols"]));$i++)
		{
			if( strripos($table["cols"][$i],"active") || strripos($table["cols"][$i],"enabled") )
			{
				$crud[] ='content.'.$table["cols"][$i].'.type = "Radio"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.label = "Exibir"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.class = "'.$table["cols"][$i].'"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.value = "0"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.options[] = "execute->Admin_Models_Cadastro::activeValues"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.rules.requerid = "none"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.rules.function = "none()"' . "\n";
				$crud[] ="\n";
			}
			elseif( 
				strripos($table["cols"][$i],"file")   || 
				strripos($table["cols"][$i],"photo")  ||
				strripos($table["cols"][$i],"imagem") ||
				strripos($table["cols"][$i],"image") 
			   )
			{
				$crud[] ='content.'.$table["cols"][$i].'.type = "File"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.label = "Imagem"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.class = "'.$table["cols"][$i].'"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.rules.requerid = "file"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.rules.function = "upload(/public/uploads/)"' . "\n";
				$crud[] ="\n";
			}
			elseif( strripos($table["cols"][$i],"insertDate") )
			{
				$crud[] ='content.'.$table["cols"][$i].'.type = "DateInsert"' . "\n";
				$crud[] ="\n";
			}
			elseif( strripos($table["cols"][$i],"updateDate") )
			{
				$crud[] ='content.'.$table["cols"][$i].'.type = "DateUpdate"' . "\n";
				$crud[] ="\n";
			}
			else
			{
				$crud[] ='content.'.$table["cols"][$i].'.type = "Text"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.label = "'.ucfirst(substr($table["cols"][$i],4,666)).'"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.class = "'.$table["cols"][$i].'"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.maxlength = "'.$table["metadata"][$table["cols"][$i]]["LENGTH"].'"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.rules.requerid = "none"' . "\n";
				$crud[] ='content.'.$table["cols"][$i].'.rules.function = "none()"' . "\n";
				$crud[] ="\n";
			}
		}
		$crud[] ='content.submite.type  = "Submit"' . "\n";
		$crud[] ='content.submite.class = "submite"' . "\n";
		$crud[] ='content.submite.value = "Salvar"' . "\n";
		$crud[] ="\n";
		
		$crud[] ='content.cancel.type  = "Button"' . "\n";
		$crud[] ='content.cancel.class = "voltar"' . "\n";
		$crud[] ='content.cancel.value = "Voltar"' . "\n";
		$crud[] ="\n";
		$crud[] ="\n";
		
		$crud[] ='#datagrid' . "\n";
		$crud[] ='query = "SELECT * FROM '. $params['tb_name'] .'"' . "\n";
		$crud[] ="\n";
		$crud[] ='info.title = ""' . "\n";
		$crud[] ='info.size = "30"' . "\n";
		$crud[] ='info.read_only = "0"' . "\n";
		$crud[] ="\n";
		
		for($i = 1;$i < (count($table["cols"]));$i++)
		{
			if( strripos($table["cols"][$i],"active") )
			{
				$crud[] ='columns.'.$table["cols"][$i].'.header = "Exibir"' . "\n";
				$crud[] ='columns.'.$table["cols"][$i].'.function = "set(/admin/' . $params['contro_name'] . '/swap/)"' . "\n";
			}
			else
			{
				$crud[] ='columns.'.$table["cols"][$i].'.header = "'.ucfirst(substr($table["cols"][$i],4,666)).'"' . "\n";
			}
		}
		$crud[] ="\n";
		$crud[] ='tools.editar = admin/' . $params['contro_name'] . '/form/id/' . "\n";
		$crud[] ='tools.excluir = admin/' . $params['contro_name'] . '/grid/id/' . "\n";
		$crud[] ="\n";
		$crud[] ="\n";
		$crud[] ="\n";
		$crud[] ='
#
# SAMPLES
#
#---RULES FUNCTIONS
#//UPLOAD
#requerid = "file",
#function = "upload(/public/uploads/)",
#//-
#//CRIPTOGRAFIA
#requerid = "none",
#function = "md5(?)",
#
#EXEC FUNCTIONS
#options[] = "execute->Default_Models_' . $params['contro_name'] . '::static_method"
#
#---FUNCTIONS GRID
#function = "phpImage(/public/uploads/,200,100)"
#function = "set(/admin/' . $params['contro_name'] . '/swap/)"
#function = "link(/admin/' . $params['contro_name'] . '/grid/rel/)"
#function = "array(0:azul,1:verde,2:vermelho)"
# 
#---TOOLS OPTIONS
#tools.editar  = "admin/' . $params['contro_name'] . '/form/id/",
#tools.excluir = "admin/' . $params['contro_name'] . '/grid/id/",
#tools.rel     = "admin/' . $params['contro_name'] . '/grid/col/",
#tools.move    = "admin/' . $params['contro_name'] . '/saveorder/id/"
#
#
#---FORM ITENS SAMPLES
#
#-- INPUT TEXT
#content.cad_name.type                             = "Text"
#content.cad_name.label                            = "Nome"
#content.cad_name.class                            = "cad_name"
#content.cad_name.rules.requerid                   = "none"
#content.cad_name.rules.function                   = "none()"
#
#-- INPUT PASSWORD
#content.cad_password.type                         = "Password"
#content.cad_password.label                        = "Senha"
#content.cad_password.class                        = "cad_password"
#content.cad_password.rules.requerid               = "none"
#content.cad_password.rules.function               = "none()"
#
#-- INPUT TEXTAREA
#content.cad_message.type                          = "Textarea"
#content.cad_message.label                         = "Mensagem"
#content.cad_message.class                         = "cad_message"
#content.cad_message.cols                          = "40"
#content.cad_message.rows                          = "3"
#content.cad_message.rules.requerid                = "none"
#content.cad_message.rules.function                = "none()"
#
#-- INPUT FILE
#content.cad_photo.type                            = "File"
#content.cad_photo.label                           = "Foto"
#content.cad_photo.class                           = "cad_photo"
#content.cad_photo.rules.requerid                  = "file"
#content.cad_photo.rules.function                  = "upload(/public/uploads/)"
#
#-- INPUT CHECKBOX
#content.cad_week.type                             = "Checkbox"
#content.cad_week.label                            = "Dias"
#content.cad_week.class                            = "cad_gender"
#content.cad_week.options[]                        = ""
#content.cad_week.rules.requerid                   = "none"
#content.cad_week.rules.function                   = "none()"
#
#-- INPUT RADIO BUTTON
#content.cad_gender.type                           = "Radio"
#content.cad_gender.label                          = "Sexo"
#content.cad_gender.class                          = "cad_gender"
#content.cad_gender.options[]                      = ""
#content.cad_gender.rules.requerid                 = "none"
#content.cad_gender.rules.function                 = "none()"
#
#-- INPUT SELECT
#content.cad_maritalStatus.type                    = "Select"
#content.cad_maritalStatus.label                   = "Estado Civil"
#content.cad_maritalStatus.class                   = "cad_maritalStatus"
#content.cad_maritalStatus.options[]               = ""
#content.cad_maritalStatus.rules.requerid          = "none"
#content.cad_maritalStatus.rules.function          = "none()"
#
#-- INPUT HIDDEN
#content.cad_test.type                             = "Hidden"
#content.cad_test.value                            = "true"
#content.cad_test.rules.requerid                   = "none"
#content.cad_test.rules.function                   = "none()"
#
#-- INPUT DATA DE INSERÇÃO
#content.cad_insertDate.type                       = "DateInsert"
#
#-- INPUT DATA DE ATUALIZAÇÃO
#content.cad_updateDate.type                       = "DateUpdate"
#
#-- INPUT CAPTCHA
#content.captcha.type                              = "Captcha"
#content.captcha.label                             = "Digite o que você vê"
#content.captcha.background                        = "fundo.png"
#content.captcha.font                              = "fonte.gdf"
#
#-- INPUT SUBMIT IMAGE
#content.image.type                                = "Image"
#content.image.class                               = "submite"
#content.image.src                                 = "/public/default/images/enviar.jpg"
#
#-- INPUT RESET
#content.reset.type                                = "Reset"
#content.reset.class                               = "submite"
#content.reset.value                               = "Limpar"
#
#-- INPUT BUTTON
#content.cancel.type                               = "Button"
#content.cancel.class                              = "submite"
#content.cancel.value                              = "Cancelar"
#
#-- INPUT SUBMIT
#content.submite.type                              = "Submit"
#content.submite.class                             = "submite"
#content.submite.value                             = "Enviar"
#
#-- Fieldset
#content.cendereco.type                            = "Fieldset"
#content.cendereco.class                           = "content-endereco"
#
#-- Label
#content.seu_endereco.type                         = "Label"
#content.seu_endereco.label                        = "Seu Endereço"
#content.seu_endereco.class                        = "seu_endereco"
#';
		$result = "";
		foreach( $crud as $key => $value )
		{
			$replace  = "";
			$posit    = strrpos($value,"=");
			$increase = 50 - (int) $posit;
			for($i=0;$i < $increase;$i++)
				$replace .= " ";
			
			$result .= str_replace("=",$replace . "=",$value);
		}
		
		return $result;
	}
	
	
	public static function pages($params)
	{
		$crud  = '[crudconfig]
table.name                                        = "fm_pages"
table.key                                         = "pag_id"

form.method                                       = "post"
form.id                                           = "fPages"
form.class                                        = "fPages"
form.enctype                                      = "multipart/form-data"
form.action                                       = "/admin/pages/insert"

content.pag_name.type                             = "Text"
content.pag_name.label                            = "Nome da Página:"
content.pag_name.class                            = "pag_name"
content.pag_name.value                            = ""
content.pag_name.options[]                        = ""
content.pag_name.rules.requerid                   = "none"
content.pag_name.rules.function                   = "none()"

content.pag_order.type                            = "Hidden"
content.pag_order.label                           = "pag_order:"
content.pag_order.class                           = "pag_order"
content.pag_order.value                           = "0"
content.pag_order.options[]                       = ""
content.pag_order.rules.requerid                  = "none"
content.pag_order.rules.function                  = "none()"

content.pag_insertDate.type                       = "DateInsert"
content.pag_insertDate.value                      = ""
content.pag_insertDate.rules.requerid             = "none"
content.pag_insertDate.rules.function             = "none()"

content.pag_updateDate.type                       = "DateUpdate"
content.pag_updateDate.value                      = ""
content.pag_updateDate.rules.requerid             = "none"
content.pag_updateDate.rules.function             = "none()"

content.pag_active.type                           = "Radio"
content.pag_active.label                          = "Exibir:"
content.pag_active.class                          = "pag_active"
content.pag_active.value                          = ""
content.pag_active.options[]                      = "Não"
content.pag_active.options[]                      = "Sim"
content.pag_active.rules.requerid                 = "none"
content.pag_active.rules.function                 = "none()"

content.submit.type                               = "Submit"
content.submit.label                              = "submite"
content.submit.class                              = "submite"
content.submit.value                              = "Salvar"

content.voltar.type                               = "Button"
content.voltar.label                              = "voltar"
content.voltar.class                              = "voltar"
content.voltar.value                              = "Voltar"


#datagrid
query                                             = "SELECT * FROM fm_pages"

info.title                                        = ""
info.size                                         = "30"
info.read_only                                    = "0"

columns.pag_name.header                           = "Nome da página"
columns.pag_active.header                         = "Exibir"
columns.pag_active.function                       = "set(/admin/pages/swap/)"

tools.editar                                      = admin/pages/form/id/
tools.excluir                                     = admin/pages/grid/id/';
		return $crud;
	}
	

	public static function sections($params)
	{
		$crud  = '[crudconfig]
table.name                                        = "fm_sections"
table.key                                         = "sec_id"

form.method                                       = "post"
form.id                                           = "fSections"
form.class                                        = "fSections"
form.enctype                                      = "multipart/form-data"
form.action                                       = "/admin/sections/insert"

content.pag_id.type                               = "Select"
content.pag_id.label                              = "Pagina do site:"
content.pag_id.class                              = "pag_id"
content.pag_id.value                              = ""
content.pag_id.options[]                          = "execute->Admin_Models_Pages::get_pages"
content.pag_id.rules.requerid                     = "none"
content.pag_id.rules.function                     = "none()"

content.sec_name.type                             = "Text"
content.sec_name.label                            = "Nome da Sessão:"
content.sec_name.class                            = "sec_name"
content.sec_name.value                            = ""
content.sec_name.options[]                        = ""
content.sec_name.rules.requerid                   = "none"
content.sec_name.rules.function                   = "none()"

content.sec_order.type                            = "Hidden"
content.sec_order.label                           = "sec_order:"
content.sec_order.class                           = "sec_order"
content.sec_order.value                           = ""
content.sec_order.options[]                       = ""
content.sec_order.rules.requerid                  = "none"
content.sec_order.rules.function                  = "none()"

content.sec_insertDate.type                       = "DateInsert"
content.sec_insertDate.value                      = ""
content.sec_insertDate.rules.requerid             = "none"
content.sec_insertDate.rules.function             = "none()"

content.sec_updateDate.type                       = "DateUpdate"
content.sec_updateDate.value                      = ""
content.sec_updateDate.rules.requerid             = "none"
content.sec_updateDate.rules.function             = "none()"

content.sec_active.type                           = "Radio"
content.sec_active.label                          = "Exibir:"
content.sec_active.class                          = "sec_active"
content.sec_active.value                          = ""
content.sec_active.options[]                      = "Não"
content.sec_active.options[]                      = "Sim"
content.sec_active.rules.requerid                 = "none"
content.sec_active.rules.function                 = "none()"

content.submit.type                               = "Submit"
content.submit.label                              = "submite"
content.submit.class                              = "submite"
content.submit.value                              = "Salvar"

content.voltar.type                               = "Button"
content.voltar.label                              = "voltar"
content.voltar.class                              = "voltar"
content.voltar.value                              = "Voltar"


#datagrid
query                                             = "SELECT * FROM fm_sections a, fm_pages b WHERE a.pag_id = b.pag_id ORDER BY a.sec_order ASC, b.pag_order ASC"

info.title                                        = ""
info.size                                         = "30"
info.read_only                                    = "0"

columns.pag_name.header                           = "Página"
columns.sec_name.header                           = "Sessão"
columns.sec_active.header                         = "Exibir"
columns.sec_active.function                       = "set(/admin/sections/swap/)"

tools.rel                                         = admin/content/grid/rel/
tools.editar                                      = admin/sections/form/id/
tools.excluir                                     = admin/sections/grid/id/';
		return $crud;
	}
	
	public static function content($params)
	{
		$crud  = '[crudconfig]
table.name                                        = "fm_content"
table.key                                         = "con_id"

form.method                                       = "post"
form.id                                           = "fContent"
form.class                                        = "fContent"
form.enctype                                      = "multipart/form-data"
form.action                                       = "/admin/content/insert"

content.sec_id.type                               = "Hidden"
content.sec_id.label                              = "sec_id:"
content.sec_id.class                              = "sec_id"
content.sec_id.value                              = "get_param->rel"
content.sec_id.options[]                          = ""
content.sec_id.rules.requerid                     = "none"
content.sec_id.rules.function                     = "none()"

content.con_folder.type                           = "Hidden"
content.con_folder.label                          = "con_folder:"
content.con_folder.class                          = "con_folder"
content.con_folder.value                          = "function->randon_date_name"
content.con_folder.options[]                      = ""
content.con_folder.rules.requerid                 = "none"
content.con_folder.rules.function                 = "none()"

content.con_title.type                            = "Text"
content.con_title.label                           = "Titulo:"
content.con_title.class                           = "con_title"
content.con_title.value                           = ""
content.con_title.options[]                       = ""
content.con_title.rules.requerid                  = "none"
content.con_title.rules.function                  = "none()"

content.con_body.type                             = "Textarea"
content.con_body.label                            = "Conteudo:"
content.con_body.class                            = "con_body"
content.con_body.value                            = ""
content.con_body.options[]                        = ""
content.con_body.rules.requerid                   = "none"
content.con_body.rules.function                   = "none()"

content.con_order.type                            = "Hidden"
content.con_order.label                           = "con_order:"
content.con_order.class                           = "con_order"
content.con_order.value                           = "0"
content.con_order.options[]                       = ""
content.con_order.rules.requerid                  = "none"
content.con_order.rules.function                  = "none()"

content.con_insertDate.type                       = "DateInsert"
content.con_insertDate.value                      = ""
content.con_insertDate.rules.requerid             = "none"
content.con_insertDate.rules.function             = "none()"

content.con_updateDate.type                       = "DateUpdate"
content.con_updateDate.value                      = ""
content.con_updateDate.rules.requerid             = "none"
content.con_updateDate.rules.function             = "none()"

content.con_active.type                           = "Radio"
content.con_active.label                          = "Exibir:"
content.con_active.class                          = "con_active"
content.con_active.value                          = ""
content.con_active.options[]                      = "Não"
content.con_active.options[]                      = "Sim"
content.con_active.rules.requerid                 = "none"
content.con_active.rules.function                 = "none()"

content.submit.type                               = "Submit"
content.submit.label                              = "submite"
content.submit.class                              = "submite"
content.submit.value                              = "Salvar"

content.voltar.type                               = "Button"
content.voltar.label                              = "voltar"
content.voltar.class                              = "voltar"
content.voltar.value                              = "Voltar"

content.JSFolder.type                             = "JSFolder"
content.JSFolder.colum                            = "con_folder"


#datagrid
query                                             = "SELECT * FROM fm_content a, fm_sections b WHERE a.sec_id = b.sec_id AND b.sec_id = ? ORDER BY a.con_order ASC, b.sec_order ASC"

info.title                                        = ""
info.size                                         = "30"
info.read_only                                    = "0"

columns.sec_name.header                           = "Sessão"
columns.con_title.header                          = "Titulo"
columns.con_active.header                         = "Exibir"
columns.con_active.function                       = "set(/admin/content/swap/)"

tools.editar                                      = admin/content/form/id/
tools.excluir                                     = admin/content/grid/id/';
		return $crud;
	}
}